package com.ceba.test.base.utils;

import abc.docs.Document;
import abc.util.DateUtils;
import abc.util.FileHelper;
import abc.util.RARUtils;
import abc.util.ZipUtils;
import com.ceba.base.helper.LoggerHelper;
import com.ceba.base.utils.IDSFileUtils;
import com.ceba.base.utils.ToPdfUtils;
import com.ceba.cebautils.DateUtil;
import com.ceba.test.base.config.LogicPathUtil;
import com.ceba.test.base.configs.CebaConfig;
import com.ceba.test.base.configs.CebaPathUtil;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.*;

@SuppressWarnings("deprecation")
public class LogicCebaFileManageUtil extends CebaFileManageUtil{

	private static Logger logger = LoggerFactory.getLogger(LogicCebaFileManageUtil.class);



	/**
	 * 把压缩包解压到临时文件夹中
	 *
	 * @param zipPath
	 *            zip包路径
	 * @return 如果返回有路径，则说明解压成功，无则说明解压有问题；
	 * @throws Exception
	 */
	public static String unZipToTempFolder(String zipPath,String storageId) throws Exception {
		String fileName = IDSFileUtils.getFileName(zipPath,true);
		fileName=fileName.substring(0, fileName.lastIndexOf("."));
		String tempPath = LogicPathUtil.getPhysicalTempDirPath(storageId) + fileName + "/";
		return unZipTo(zipPath, tempPath);
	}



	/**
	 * 清空临时文件夹
	 *
	 * @throws Exception
	 */
	public static Boolean emptyTempFolder(String storageId) {
		String tempPath = LogicPathUtil.getPhysicalTempDirPath(storageId);
		Collection<String> fileAnDirs;
		try {
			fileAnDirs = FileHelper.listFilesAndDirectorys(tempPath, "");
			Iterator<String> iterator = fileAnDirs.iterator();
			while (iterator.hasNext()) {
				String path = iterator.next();
				if (!path.replace("/", "").replace("\\", "").equals(tempPath.replace("/", "").replace("\\", ""))) {
					if (FileHelper.exists(path)) {
						if (!FileHelper.deleteFile(path)) {
							FileHelper.deleteDirectory(path);
						}
					}
				}
			}
		} catch (Exception e) {
			LoggerHelper.error(logger, e, "");
			return false;
		}

		return true;

	}
}
